﻿using Bigger.Framework;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Bigger.Study.ConsoleApp
{
    public class TcpListenerExample : IExample
    {
        public bool IsRunning { get; set; }
        public TcpListenerExample()
        {
            this.IsRunning = true;
        }
        public void Execute()
        {
            TcpListener objTcpListener = new TcpListener(IPAddress.Parse("127.0.0.1"), 9001);
            objTcpListener.Start();

            ConsoleHelper.WriteLineYellow("开始监听");
            ThreadPool.QueueUserWorkItem((state) =>
            {

                while (this.IsRunning)
                {
                    try
                    {
                        //当接收到客户端请求时，确认与客户端的连接
                        if (objTcpListener.Pending())
                        {
                            TcpClient objTcpClient = objTcpListener.AcceptTcpClient();
                            NetworkStream objNetworkStream = objTcpClient.GetStream();
                            ThreadPool.QueueUserWorkItem((state1) =>
                            {
                                //StreamReader objStreamReader = new StreamReader(objNetworkStream);
                                //StreamWriter objStreamWriter = new StreamWriter(objNetworkStream);

                                //readLine = objStreamReader.ReadLine();
                                //ConsoleHelper.WriteLineYellow("服务端接受信息：" + objStreamReader.ReadToEnd());
                                //string readLine;
                                //while (!objStreamReader.EndOfStream)
                                //{
                                //    readLine = objStreamReader.ReadLine();
                                //    //readLine = objStreamReader.ReadLine();
                                //    ConsoleHelper.WriteLineYellow("服务端接受信息：" + readLine);

                                //    objStreamWriter.WriteLine("服务端发送编码：" + Guid.NewGuid());
                                //    objStreamWriter.Flush();
                                //    objStreamWriter.WriteLine("服务端发送时间：" + DateTime.Now);
                                //    objStreamWriter.Flush();
                                //}

                                BinaryReader objBinaryReader = new BinaryReader(objNetworkStream);


                                //if (objTcpClient.Available > 0)
                                //{
                                //    byte[] bytes1 = new byte[objTcpClient.Available];

                                //    int readBytes = 0;
                                //    while (readBytes < bytes1.Length)
                                //    {
                                //        readBytes += objBinaryReader.Read(bytes1, readBytes, (bytes1.Length - readBytes));
                                //    }
                                //}


                                //int length = objBinaryReader.ReadInt32();

                                //bool flag = objBinaryReader.ReadBoolean();

                                //byte[] bytes = objBinaryReader.ReadBytes(length);

                                string result = objBinaryReader.ReadString();


                                TestDemoContext1 objTestDemoContext1 = result.NewJsonDeserialize<TestDemoContext1>();



                                ConsoleHelper.WriteLineYellow("服务端接受信息：" + result);

                                Thread.Sleep(1000 * 3);
                            });
                        }
                    }
                    catch (Exception objException)
                    {
                        ConsoleHelper.WriteLineRed("服务端发生异常！结束运行");
                        break;
                        //this.ExceptionQueue.Enqueue(objException);
                    }
                }
            });
        }


        [Serializable]
        public class TestDemoContext1
        {
            public string S1 { get; set; }
            public int I1 { get; set; }
            public string Url { get; set; }
            public IDictionary<string, object> Parameters { get; set; }
        }

        [Serializable]
        public class Person1
        {
            public string Name { get; set; }
            public int Age { get; set; }
        }
    }
}
